# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/10/13 6:01 下午
# @Author  : 白辉东
# @Mail    : masterbai2018@outlook.com
# @File    : serializers.py
# @Software: PyCharm
from django.core.exceptions import ObjectDoesNotExist
from rest_framework import status
from rest_framework.generics import ListAPIView, CreateAPIView
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from rest_framework.generics import RetrieveAPIView, UpdateAPIView, DestroyAPIView
from .models import CaseModule, SpeakerProduct, TestCase, SpeakerParams
from .serializers import CaseModuleModelSerializer, SpeakerProductModelSerializer, TestCaseModelSerializer, \
    SpeakerTaskInfoSerializer, SpeakerAddSerializer, CaseModuleSerializer, SpeakerParamsSerializer
from rest_framework.permissions import IsAuthenticated


# class CaseModuleListAPIView(ListAPIView):
#     # 1、query_set查询数据
#     queryset = CaseModule.objects.all()
#     # 2、创建序列化器
#     serializer_class = CaseModuleModelSerializer
#     # 3、指定权限
#     # permission_classes = [IsAuthenticated]


# class CaseModuleDetailAPIView(RetrieveAPIView):
#     # 1、query_set查询数据
#     queryset = CaseModule.objects.all()
#     # 2、创建序列化器
#     serializer_class = CaseModuleModelSerializer
#     # 3、指定权限
#     # permission_classes = [IsAuthenticated]


class SpeakerProductListAPIView(ListAPIView):
    # 权限检查
    permission_classes = [IsAuthenticated]

    # 1、查询所有的音箱列表
    queryset = SpeakerProduct.objects.all()
    # 2、创建序列化器
    serializer_class = SpeakerProductModelSerializer
    # 3、指定权限
    # permission_classes = [IsAuthenticated]


# class SpeakerProductDetailAPIView(RetrieveAPIView, UpdateAPIView, DestroyAPIView):
#     # 1、query_set查询数据
#     queryset = SpeakerProduct.objects.all()
#     # 2、创建序列化器
#     serializer_class = SpeakerProductModelSerializer
#     # 3、指定权限
#     # permission_classes = [IsAuthenticated]


class TestCaseListAPIView(ListAPIView):
    # 权限检查
    permission_classes = [IsAuthenticated]

    queryset = TestCase.objects.all()
    serializer_class = TestCaseModelSerializer

    # 重写get_queryset
    def get_queryset(self):
        product_id = self.request.query_params.get('product_id')
        module_id = self.request.query_params.get('module_id')
        if module_id == '0':
            return TestCase.objects.filter(module__speaker=product_id)
        else:
            return TestCase.objects.filter(module=module_id, module__speaker=product_id)


class TestCaseDetailAPIView(CreateAPIView, DestroyAPIView):
    # 权限检查
    permission_classes = [IsAuthenticated]

    queryset = TestCase.objects.all()
    serializer_class = TestCaseModelSerializer


class TestCaseDeleteAPIView(DestroyAPIView):
    # 权限检查
    permission_classes = [IsAuthenticated]

    queryset = TestCase.objects.all()
    serializer_class = TestCaseModelSerializer

    # 重写删除返回状态码
    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()
        self.perform_destroy(instance)
        return Response({'message': '删除成功！'}, status=status.HTTP_200_OK)


class TaskInfoAPIView(ListAPIView):
    # 权限检查
    permission_classes = [IsAuthenticated]

    queryset = SpeakerProduct.objects.all()
    serializer_class = SpeakerTaskInfoSerializer


class SpeakerModuleCreateView(ModelViewSet):
    # 权限检查
    permission_classes = [IsAuthenticated]

    queryset = SpeakerProduct.objects.all()
    serializer_class = SpeakerAddSerializer

    def create(self, request, *args, **kwargs):
        s_name = request.data.get("speaker_name")
        rom_version = request.data.get("rom_version")
        system_version = request.data.get("system_version")
        try:
            SpeakerProduct.objects.get(speaker_name=s_name)
            return Response({"message": "存在相同产品名"}, status=status.HTTP_400_BAD_REQUEST)
        except ObjectDoesNotExist:
            pass

        serializer = self.get_serializer(data={"speaker_name": s_name, "rom_version": rom_version, "system_version": system_version})
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        speaker_id = SpeakerProduct.objects.get(speaker_name=s_name).id

        for i in request.data.get("modules"):
            case_data = {"module_name": i}
            case_module_serializer = CaseModuleSerializer(data=case_data)
            case_module_serializer.is_valid(raise_exception=True)
            case_module_serializer.save(speaker_id=speaker_id)

        return Response(status=status.HTTP_200_OK, headers=headers)


class TaskAllCaseAPIView(ListAPIView):
    queryset = TestCase.objects.all()

    serializer_class = TestCaseModelSerializer

    # 重写get_queryset
    def get_queryset(self):
        task_id = self.request.query_params.get('task_id')
        is_smoke = self.request.query_params.get('is_smoke')
        if is_smoke == 'True':
            return TestCase.objects.filter(module__taskmodulemodel__task_id=task_id, is_smoke=True)
        elif is_smoke == 'False':
            return TestCase.objects.filter(module__taskmodulemodel__task_id=task_id)
        else:
            return []


class TestCaseRetrieveAPIView(RetrieveAPIView):
    queryset = TestCase.objects.all()
    serializer_class = TestCaseModelSerializer


class SpeakerParamsViewSet(ModelViewSet):
    def retrieve(self, request, pk=None, *args, **kwargs):
        print('222222')
        pass

    def get_queryset(self):
        pass
